package enumandlambda;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author shkstart
 * @create 2022-12-10-22:41
 */
public class CASDemo1 {
    private static int number = 0;
    private static AtomicInteger atomicInteger = new AtomicInteger(0);
    private final static int MAX_COUNT = 100000;

    public static void main(String[] args) throws InterruptedException {
        //++
        Thread t1 = new Thread(()->{
            for (int i = 0; i < MAX_COUNT; i++) {
//                number++;
                atomicInteger.getAndIncrement();//相当于++
            }
        });
        t1.start();

        //--
        Thread t2 = new Thread(()->{
            for (int i = 0; i < MAX_COUNT; i++) {
                //number--;
                atomicInteger.getAndDecrement();//相当于--
            }
        });
        t2.start();

        t1.join();
        t2.join();

        System.out.println(atomicInteger.get());
    }
}
